package enterprise.manage.web.controller.order;

import enterprise.manage.dao.OrderDao;
import enterprise.manage.service.extremetable.ExtremeTablePage;
import enterprise.manage.util.Constants;
import enterprise.manage.util.TimeUtils;
import org.apache.commons.lang.StringUtils;
import org.extremecomponents.table.limit.Limit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import org.springside.modules.orm.Page;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Enumeration;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User:zhenmingyue
 * Date: 2012-11-24
 * Desc:
 */
public class OrderListController extends AbstractController {
    @Autowired
    OrderDao orderDao;
    public static final String DEFAULT_PAGE = "order/list";

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Limit limit = ExtremeTablePage.getLimit(request, response, Constants.DEFAULT_PAGE_SIZE);
        int pageNo = limit.getPage();
        int pageSize = limit.getCurrentRowsDisplayed();
        String requestBeginDate = request.getParameter("queryBegin");  //开始时间
        String requestEndDate = request.getParameter("queryEnd");      //结束时间
        String userName = request.getParameter("userName");  //
        String orderNo = request.getParameter("orderNo");  //
        String consignee = request.getParameter("consignee");  //
        String consigneeAddress = request.getParameter("consigneeAddress");  //
        String mobile = request.getParameter("mobile");  //
        String status = request.getParameter("status");  //
        String logisticsOrderId = request.getParameter("logisticsOrderId");  //
        String queryTimeTypeId = request.getParameter("queryTimeTypeId");  //

        Calendar queryBegin = null;
        Calendar queryEnd = null;
        try {
            if (StringUtils.isNotBlank(requestBeginDate) && StringUtils.isNotBlank(requestEndDate)) {
                queryBegin = TimeUtils.getCalendar(requestBeginDate);
                queryEnd = TimeUtils.getCalendar(requestEndDate);
            } else {
                queryBegin = TimeUtils.getCalendar("", "", 0, " 0:0:0");
                queryEnd = TimeUtils.getCalendar("", "", 0, " 23:59:59");
                //默认初始化页面 及查询
                requestBeginDate = TimeUtils.getDateString(queryBegin);
                requestEndDate =   TimeUtils.getDateString(queryEnd);
                queryTimeTypeId = "3";

                request.setAttribute("queryBegin",requestBeginDate);
                request.setAttribute("queryEnd",requestEndDate);
                request.setAttribute("queryTimeTypeId",queryTimeTypeId);
            }
        } catch (ParseException e) {
            logger.error(e);
            request.setAttribute("totalRows", 0);
            return new ModelAndView(DEFAULT_PAGE);
        }
        Page page = orderDao.getOrderPage(pageNo, pageSize, queryBegin, queryEnd,
                orderNo,   userName,   consignee,   consigneeAddress,
               mobile,   status,   logisticsOrderId,
               queryTimeTypeId);
        request.setAttribute("items", page.getResult());
        request.setAttribute("totalRows", page.getTotalCount());
        keepRequestAttribute(request);
        return new ModelAndView(DEFAULT_PAGE);
    }
    public void keepRequestAttribute(HttpServletRequest request){
         Enumeration e = request.getParameterNames();
        while (e.hasMoreElements()){
           String key = e.nextElement().toString();
           String val =  request.getParameter(key);
          request.setAttribute(key,val);
        }
    }
}
